#!/bin/bash

echo "关闭防火墙和selinux"
systemctl disable --now firewalld NetworkManager
setenforce 0
sed -ri '/^[^#]*SELINUX=/s#=.+$#=disabled#' /etc/selinux/config

echo "关闭 dnsmasq"
systemctl disable --now dnsmasq

echo "关闭swap"
swapoff -a && sysctl -w vm.swappiness=0
sed -ri '/^[^#]*swap/s@^@#@' /etc/fstab

echo "安装wget git jq psmisc "
yum install epel-release -y
yum install wget git  jq psmisc -y

echo "你需要重新，请执行reboot"


echo "检查perl，这是内核安装依赖项"
[ ! -f /usr/bin/perl ] && yum install perl -y

echo "导入elrepo 的yum 源"
rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
rpm -Uvh http://www.elrepo.org/elrepo-release-7.0-2.el7.elrepo.noarch.rpm

echo "安装内核版本4.18.9-1"
export Kernel_Vsersion=4.18.9-1
wget  http://mirror.rc.usf.edu/compute_lock/elrepo/kernel/el7/x86_64/RPMS/kernel-ml{,-devel}-${Kernel_Vsersion}.el7.elrepo.x86_64.rpm
yum localinstall -y kernel-ml*

echo "检查内核模块是否有nf_conntrack_ipv4"
find /lib/modules -name '*nf_conntrack_ipv4*' -type f

echo "修改内核启动顺序"
grub2-set-default  0 && grub2-mkconfig -o /etc/grub2.cfg

echo "下载docker检查的脚本check-config.sh"
curl https://raw.githubusercontent.com/docker/docker/master/contrib/check-config.sh > check-config.sh
bash ./check-config.sh

echo "修复docker检查脚本报错"
grubby --args="user_namespace.enable=1" --update-kernel="$(grubby --default-kernel)"

echo "你需要重新，请执行reboot"

echo "安装ipvs"
yum install ipvsadm ipset sysstat conntrack libseccomp -y

echo "加载内核模块"
:> /etc/modules-load.d/ipvs.conf
module=(
  ip_vs
  ip_vs_lc
  ip_vs_wlc
  ip_vs_rr
  ip_vs_wrr
  ip_vs_lblc
  ip_vs_lblcr
  ip_vs_dh
  ip_vs_sh
  ip_vs_fo
  ip_vs_nq
  ip_vs_sed
  ip_vs_ftp
  )

for kernel_module in ${module[@]};do
    /sbin/modinfo -F filename $kernel_module |& grep -qv ERROR && echo $kernel_module >> /etc/modules-load.d/ipvs.conf || :
done
systemctl enable --now systemd-modules-load.service

echo "最后设置hostname以及hosts文件，同步时区CST，节点时间不一致会出现问题"
yum install -y ntpdate
rm -f /etc/localtime 
cp /usr/share/zoneinfo/Asia/Shanghai /etc/localtime 
ntpdate us.pool.ntp.org

cat >/etc/sysctl.conf<<EOF
net.bridge.bridge-nf-call-ip6tables = 1
net.bridge.bridge-nf-call-iptables = 1
net.ipv4.ip_forward = 1
EOF

sysctl -p /etc/sysctl.conf

echo "安装docker"

mkdir -p /etc/docker/
cat>/etc/docker/daemon.json<<EOF
{
  "registry-mirrors": ["https://fz5yth0r.mirror.aliyuncs.com"],
  "insecure-registries": ["172.17.1.10"],
  "storage-driver": "overlay2",
  "storage-opts": [
    "overlay2.override_kernel_check=true"
  ],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "100m",
    "max-file": "3"
  }
}
EOF
systemctl daemon-reload

yum install -y epel-release bash-completion && cp /usr/share/bash-completion/completions/docker /etc/bash_completion.d/
systemctl enable --now docker


echo "安装kubeadm kubelet kubectl工具"
cat > /etc/yum.repos.d/k8s.repo<<EOF
[kubernetes]
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1
gpgcheck=0
EOF

yum makecache; yum install kubeadm-1.11.3 kubelet-1.11.3 kubectl-1.11.3 -y

# 解决kubelet自动联想问题
yum install bash-completion -y 
systemctl enable kubelet


curl  -s https://zhangguanzhang.github.io/bash/pull.sh | bash -s -- search quay.io/calico/cni:v3.3.1 搜索对应的镜像

curl  -s https://zhangguanzhang.github.io/bash/pull.sh | bash -s -- quay.io/calico/cni:v3.3.1 下载对应的镜像